//
//  CZEmotionBottomView.swift
//  CZWeibo
//
//  Created by 左凯旭 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//定义一个参数
private let tagNumber = 2000
//设置枚举
enum CZEmotionBottomViewType:Int {
    
    case Default = 0
    case Emoticon = 1
    case Lxh = 2

}


class CZEmotionBottomView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //定义一个当前点击的Button
    var currentButton:UIButton?
    
    //声明一个闭包 01
    var closure:((type:CZEmotionBottomViewType)->())?
    
    //MARK: - 提供方法，供外界调用
    func setupCurrentButton(tag:Int){
       //得到当前BUtton
       let button = viewWithTag(tag+tagNumber) as! UIButton
        if currentButton == button {
            return
        }
        //取消上一个按钮的选中状态
        currentButton?.selected = false
        
        
        button.selected = true
        
        currentButton = button
    }
    
    
    
    
    //MARK: - 底部按钮点击的监听
    func bottomButtonClick(button:UIButton){
        if currentButton == button {
            return
        }
        //取消上一个按钮的选中状态
        currentButton?.selected = false
        
        
        button.selected = true
        
        currentButton = button
        
        //执行闭包
        closure?(type:CZEmotionBottomViewType(rawValue: button.tag-tagNumber)!)
        
    }
    
    
    
    //MARK: - 设置UI
    func setupUI(){
        
//        backgroundColor = RandomColor()
        //添加控件
        let defaultBtn = addBottomButtons("left", title: "默认",type: .Default)
        let emoticonBtn = addBottomButtons("mid", title: "Emoji",type: .Emoticon)
        let lxhBtn = addBottomButtons("right", title: "浪小花",type: .Lxh)
        
        //添加约束
        defaultBtn.snp_makeConstraints { (make) in
            make.left.bottom.top.equalTo(self)
            make.width.equalTo(emoticonBtn)
        }
        
        emoticonBtn.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultBtn.snp_trailing)
            make.width.equalTo(lxhBtn)
        }
        
        lxhBtn.snp_makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(emoticonBtn.snp_trailing)
        }
        
    }
    
    
    //添加底部的按钮
    func addBottomButtons(imageName:String,title:String,type:CZEmotionBottomViewType)->UIButton{
        //compose_emotion_table_left_normal compose_emotion_table_left_selected
        let btn = UIButton()
        
        btn.tag = type.rawValue + tagNumber
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_normal"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: UIControlState.Selected)
        
        //设置文字
        btn.setTitle(title, forState: UIControlState.Normal)
        //设置文字大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        //文字颜色
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        
        btn.addTarget(self, action: "bottomButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 默认情况下 default 是选中状态
        if type == .Default {
            btn.selected = true
            currentButton = btn
        }
        
        //添加
        addSubview(btn)
        
        return btn
    }
    
}
